package com.zhou.project.modules.circle.circlePostsComment.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.project.modules.circle.circlePostsComment.entity.CirclePostsComment;
import com.zhou.project.modules.circle.circlePostsComment.service.CircleCommentService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("circle/comment")
@Api(tags = "[话题评论]")
public class CircleCommentController {

    @Autowired
    private CircleCommentService circleCommentService;

    @PostMapping
    @ApiOperation("评论创建")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "postsId",value = "帖子的id",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "postsText",value = "评论内容",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "pid",value = "如果是评论的评论必须携带,此pid是要评论的评论的id",required = false,dataTypeClass = Integer.class)
    })
    @PreAuthorize("hasAuthority('comment:save')")
    public void save(@RequestBody @Valid CirclePostsComment entity)
    {
        circleCommentService.save(entity);
    }

    @DeleteMapping("{textId}")
    @ApiOperation("评论删除")
    @ApiImplicitParam(name = "id",value = "评论id",required = true,dataTypeClass = Integer.class)
    @PreAuthorize("hasAuthority('comment:delete')")
    public void delete(@PathVariable int textId)
    {
        circleCommentService.removeById(textId);
    }

    @DeleteMapping("dbatch")
    @ApiOperation("评论批量删除")
    @ApiImplicitParam(name = "ids",value = "评论id集合",required = true,dataTypeClass = List.class)
    @PreAuthorize("hasAuthority('comment:dbatch')")
    public void delete(@RequestBody List<Integer> ids)
    {
        circleCommentService.removeByIds(ids);
    }

    @PostMapping("page")
    @ApiOperation("评论分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "postsId",value = "帖子的id",required = true,dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "postsText",value = "评论内容",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "pid",value = "如果是评论评论必须携带",required = false,dataTypeClass = Integer.class)
    })
    @PreAuthorize("hasAuthority('comment:page')")
    public Page<CirclePostsComment> query(@RequestBody CirclePostsComment entity){

        return circleCommentService.page(entity);
    }

    /**通过postsId删除所有评论*/
    @PreAuthorize("hasAuthority('comment:deleteAll')")
    public void deleteAll(int postsId){
        circleCommentService.deleteAll(postsId);
    }

}
